<template>

  <div class="app-container calendar-list-container">

    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="名字" v-model="listQuery.name">
      </el-input>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="电话" v-model="listQuery.phone">
      </el-input>
      <el-select clearable  @change='handleFilter' class="filter-item" style="width: 130px" v-model="listQuery.isActivation" placeholder="状态">
        <el-option label="激活" value="1">
        </el-option>
        <el-option label="未激活" value="0">
        </el-option>
      </el-select>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">新增</el-button>
      <!--<el-button class="filter-item" type="primary" :loading="downloadLoading"  icon="el-icon-download" @click="handleDownload">导出</el-button>-->
    </div>

    <el-table :data="list.slice((currentPage-1)*pageSize,currentPage*pageSize)" element-loading-text="Loading" border fit highlight-current-row >
      <el-table-column align="center" label='排序' width="110">
        <template slot-scope="scope">
          {{scope.$index}}
        </template>
      </el-table-column>
      <el-table-column label="员工ID" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.adminUserId}}
        </template>
      </el-table-column>
      <el-table-column label="名字" width="180" align="center">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="电话" width="180" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录名" width="180" align="center">
        <template slot-scope="scope">
          {{scope.row.loginName}}
        </template>
      </el-table-column>
      <!--<el-table-column label="密码" width="160" align="center">
        <template slot-scope="scope">
          {{scope.row.loginPwd}}
        </template>
      </el-table-column>-->
      <el-table-column label="时间" width="180" align="center">
        <template slot-scope="scope">
          {{scope.row.loginTime}}
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="130" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isActivation | statusFilter">{{scope.row.isActivation === 0 ? '未激活' : '激活'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column  label="操作"  align="center"  >
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)" size="mini">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding-top: 15px;text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=currentPage
        :page-sizes="[10, 20, 30]"
        :page-size=pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="80px" style='width: 400px; margin-left:50px;'>


        <el-form-item label="名字" prop="name">
          <el-input v-model="temp.name"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="temp.phone"></el-input>
        </el-form-item>

        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="temp.loginName"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="loginPwd">
          <el-input type="password" v-model="temp.loginPwd"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPaw">
          <el-input type="password" v-model="temp.confirmPaw"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="isActivation">
          <el-select class="filter-item" prop="isActivation" v-model="temp.isActivation" placeholder="请选择">
            <el-option
              v-for="item in status"
              :key="item.val"
              :label="item.name"
              :value="item.val">
            </el-option>
          </el-select>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="dialogFormVisible = false">取消</el-button>
        <el-button :loading="buttonLoading" v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
        <el-button  :loading="buttonLoading" v-else type="primary" @click="updateData">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>

  .filter-container{
    padding-bottom: 20px;
  }

</style>

<script>
import { List, Merge } from '../../api/adminUser'

export default {

  data() {
    var checkPaw = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('密码不能少于四位'))
      }
      if (this.temp.loginPwd !== this.temp.confirmPaw) { callback(new Error('密码不一致')) }
      callback()
    }

    return {
      status: [
        { name: '未激活', val: 0 },
        { name: '激活', val: 1 }
      ],
      list: null,
      listLoading: true,
      buttonLoading: false,
      listQuery: {
        name: null,
        phone: null,
        isActivation: null
      },
      temp: {
        adminUserId: null,
        name: '',
        phone: '',
        loginName: '',
        loginPwd: '',
        confirmPaw: '',
        isActivation: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      downloadLoading: false,
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        name: [{ required: true }],
        loginName: [{ required: true }],
        loginPwd: [{ required: true }],
        phone: [{ required: true }],
        isActivation: [{ required: true }],
        confirmPaw: [
          { required: true },
          { validator: checkPaw }]
      },
      total: 0,
      currentPage: 1,
      pageSize: 10
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger',
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      List(this.listQuery.name, this.listQuery.phone, this.listQuery.isActivation).then(response => {
        this.list = response.data
        this.total = response.data.length
        this.currentPage = 1
        this.listLoading = false
      })
    },
    handleFilter() {
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.resetTemp()
      this.temp = Object.assign(this.temp, row)
      this.temp.confirmPaw = this.temp.loginPwd
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    handleDownload() {

    },
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          Merge(this.temp).then(res => {
            this.temp = res.data
            this.dialogFormVisible = false
            this.buttonLoading = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.fetchData()
          }).catch(reason => {
            console.log(reason)
            this.buttonLoading = false
          })
        } else {
          console.log('验证失败')
        }
      })
    },
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          Merge(this.temp).then(res => {
            for (const v of this.list) {
              if (this.temp.adminUserId === v.adminUserId) {
                const index = this.list.indexOf(v)
                console.log(index)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.buttonLoading = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          }).catch(reason => {
            console.log(reason)
            this.buttonLoading = false
          })
        }
      })
    },
    resetTemp() {
      this.temp = {
        adminUserId: null,
        name: '',
        phone: '',
        paw: '',
        loginName: '',
        loginPwd: '',
        isActivation: 0
      }
    }
  }
}
</script>
